package org.myOrg.spetrum.io;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map.Entry;

import org.myOrg.spetrum.Driver.SpectrumUnion;
import org.myOrg.spetrum.combination.Tuple;
import org.myOrg.spetrum.spectrumStruct.Spectrum;
import org.myOrg.spetrum.spectrumStruct.SpectrumFactory;
import org.myOrg.spetrum.testObject.TestSuite;

public class OutputManagerImplement implements OutputManager {
	private PrintWriter out;

	@Override
	public void outputSpectrum(Spectrum spectrum, String path) {
		// TODO Auto-generated method stub
		try {
			out = new PrintWriter(path);
			printSpectrumName(spectrum);

			printBugNum(spectrum);
			printBug(spectrum);

			printGenTestsNum(spectrum);
			printGenSuite(spectrum);
			out.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void printSpectrumName(Spectrum spectrum) {
		out.println("alogrithm : "
				+ SpectrumFactory.getName(spectrum.getSpectrumID()));
	}

	private void printGenSuite(Spectrum spectrum) {
		TestSuite suite = spectrum.getGenerateTestCases();
		for (int i = 0; i < suite.getTestCaseNum(); i++) {
			out.println(suite.getAt(i).getStringOfTest());
		}
	}

	private void printGenTestsNum(Spectrum spectrum) {
		out.println("generated tests : "
				+ spectrum.getGenerateTestCases().getTestCaseNum());
	}

	private void printBug(Spectrum spectrum) {
		Iterator<Tuple> bugs = spectrum.getBugs().iterator();
		while (bugs.hasNext()) {
			out.println(bugs.next().toString(spectrum.getParamLength()));
		}
	}

	private void printBugNum(Spectrum spectrum) {
		out.println("deteced bugs : " + spectrum.getBugs().size());
	}

	@Override
	public void outputSpectrumUnion(SpectrumUnion uion, String path) {
		// TODO Auto-generated method stub
		try {
			out = new PrintWriter(path);
			Iterator<Entry<Integer, Spectrum>> spectrums = uion.itertor();
			while (spectrums.hasNext()) {
				Spectrum spectrum = spectrums.next().getValue();
				printSpectrumName(spectrum);
				printBugNum(spectrum);
				printGenTestsNum(spectrum);
			}

			out.println();
			
			spectrums = uion.itertor();
			while (spectrums.hasNext()) {
				Spectrum spectrum = spectrums.next().getValue();
				printSpectrumName(spectrum);
				printBug(spectrum);
				printGenSuite(spectrum);
			}
			out.flush();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
